package cn.gcyl.module.biz.controller.admin.projectprocurementinformation;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.gcyl.framework.common.pojo.PageParam;
import cn.gcyl.framework.common.pojo.PageResult;
import cn.gcyl.framework.common.pojo.CommonResult;
import cn.gcyl.framework.common.util.object.BeanUtils;
import static cn.gcyl.framework.common.pojo.CommonResult.success;

import cn.gcyl.framework.excel.core.util.ExcelUtils;

import cn.gcyl.framework.apilog.core.annotation.ApiAccessLog;
import static cn.gcyl.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.gcyl.module.biz.controller.admin.projectprocurementinformation.vo.*;
import cn.gcyl.module.biz.dal.dataobject.projectprocurementinformation.ProjectProcurementInformationDO;
import cn.gcyl.module.biz.service.projectprocurementinformation.ProjectProcurementInformationService;

@Tag(name = "管理后台 - 项目采购信息")
@RestController
@RequestMapping("/biz/project-procurement-information")
@Validated
public class ProjectProcurementInformationController {

    @Resource
    private ProjectProcurementInformationService projectProcurementInformationService;

    @PostMapping("/create")
    @Operation(summary = "创建项目采购信息")
    @PreAuthorize("@ss.hasPermission('biz:project-procurement-information:create')")
    public CommonResult<Long> createProjectProcurementInformation(@Valid @RequestBody ProjectProcurementInformationSaveReqVO createReqVO) {
        return success(projectProcurementInformationService.createProjectProcurementInformation(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新项目采购信息")
    @PreAuthorize("@ss.hasPermission('biz:project-procurement-information:update')")
    public CommonResult<Boolean> updateProjectProcurementInformation(@Valid @RequestBody ProjectProcurementInformationSaveReqVO updateReqVO) {
        projectProcurementInformationService.updateProjectProcurementInformation(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除项目采购信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('biz:project-procurement-information:delete')")
    public CommonResult<Boolean> deleteProjectProcurementInformation(@RequestParam("id") Long id) {
        projectProcurementInformationService.deleteProjectProcurementInformation(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除项目采购信息")
                @PreAuthorize("@ss.hasPermission('biz:project-procurement-information:delete')")
    public CommonResult<Boolean> deleteProjectProcurementInformationList(@RequestParam("ids") List<Long> ids) {
        projectProcurementInformationService.deleteProjectProcurementInformationListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得项目采购信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('biz:project-procurement-information:query')")
    public CommonResult<ProjectProcurementInformationRespVO> getProjectProcurementInformation(@RequestParam("id") Long id) {
        ProjectProcurementInformationDO projectProcurementInformation = projectProcurementInformationService.getProjectProcurementInformation(id);
        return success(BeanUtils.toBean(projectProcurementInformation, ProjectProcurementInformationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得项目采购信息分页")
    @PreAuthorize("@ss.hasPermission('biz:project-procurement-information:query')")
    public CommonResult<PageResult<ProjectProcurementInformationRespVO>> getProjectProcurementInformationPage(@Valid ProjectProcurementInformationPageReqVO pageReqVO) {
        PageResult<ProjectProcurementInformationDO> pageResult = projectProcurementInformationService.getProjectProcurementInformationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProjectProcurementInformationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出项目采购信息 Excel")
    @PreAuthorize("@ss.hasPermission('biz:project-procurement-information:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProjectProcurementInformationExcel(@Valid ProjectProcurementInformationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProjectProcurementInformationDO> list = projectProcurementInformationService.getProjectProcurementInformationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "项目采购信息.xls", "数据", ProjectProcurementInformationRespVO.class,
                        BeanUtils.toBean(list, ProjectProcurementInformationRespVO.class));
    }

}